package com.openvalley.cyj.examination;

import com.openvalley.cyj.examination.constant.ConstantOfDatabase;
import com.openvalley.cyj.examination.constant.ConstantOfTableUser;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class DataUserAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x000001, DataUserAbility.class.getSimpleName());
    //≈数据库对象，此处是建立一个放置在磁盘上的数据库
    private final StoreConfig storeConfig = StoreConfig.newDefaultConfig(ConstantOfDatabase.DATABASE_NAME);
    //≈连接
    RdbStore rdbStore = null;

    DatabaseHelper databaseHelper = null;

    //创建表的回调
    private RdbOpenCallback openCallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            String createTableUser = "CREATE TABLE IF NOT EXISTS " + ConstantOfTableUser.DATABASE_TABLE_NAME
                    + "(" + ConstantOfTableUser.DATABASE_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + ConstantOfTableUser.DATABASE_COLUMN_NAME + " STRING, "
                    + ConstantOfTableUser.DATABASE_COLUMN_PASSWORD + " STRING, "
                    + ConstantOfTableUser.DATABASE_COLUMN_IDENTITY + " INTEGER, "
                    + ConstantOfTableUser.DATABASE_COLUMN_AUTHORITY + " INTEGER, "
                    + ConstantOfTableUser.DATABASE_COLUMN_IS_LOGIN + " INTEGER" + ")";
//            HiLog.info(LABEL_LOG, "DatabaseAbility onCreate createTableUser = " + createTableUser);

            //创建数据库连接时创建表
            rdbStore.executeSql(createTableUser);
//            HiLog.info(LABEL_LOG, "DatabaseAbility onCreate 表创建成功。 createTable = " + createTable);
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        initDataBase();
    }

    /**
     *
     */
    private void initDataBase(){
        //数据库帮助
        databaseHelper = new DatabaseHelper(getContext());
        //给≈连接赋值
        rdbStore = databaseHelper.getRdbStore(storeConfig, 1, openCallback, null);
        HiLog.info(LABEL_LOG, "DataUserAbility onStart");
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        String tableName = uri.getLastPath();
        //因为条件（谓词）要的和给的不一样，所以需要用帮助类来帮忙转换。
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, tableName);
//        HiLog.info(LABEL_LOG, "DatabaseAbility query 查询成功。");
        return rdbStore.query(rdbPredicates, columns);
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        String tableName = uri.getLastPath();
        long resultRow = rdbStore.insert(tableName, value);
//        HiLog.info(LABEL_LOG, "DatabaseAbility insert resultRow = " + resultRow);
        //数据改动时发通知
        DataAbilityHelper.creator(getContext()).notifyChange(uri);
        return Integer.parseInt(String.valueOf(resultRow));
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        String tableName = uri.getLastPath();
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, tableName);
        int resultRow = rdbStore.delete(rdbPredicates);
//        HiLog.info(LABEL_LOG, "DatabaseAbility delete resultRow = " + resultRow);
        //数据改动时发通知
        DataAbilityHelper.creator(getContext()).notifyChange(uri);
        return resultRow;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        String tableName = uri.getLastPath();
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, tableName);
        int resultRow = rdbStore.update(value, rdbPredicates);
//        HiLog.info(LABEL_LOG, "DatabaseAbility update resultRow = " + resultRow);
        //数据改动时发通知
        DataAbilityHelper.creator(getContext()).notifyChange(uri);
        return resultRow;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}